#include <vector>
#include <map>
#include <iostream>

using namespace std;

// 350
// 数组直接存入 set.
class Intersect{
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
		// map
		map<int, int> record;
		map<int, int>::iterator iter;
		for(int i = 0; i < nums1.size(); i++){
			iter = record.find(nums1[i]);
			if(iter != record.end())	// 找到该元素
				iter->second ++;   // record[nums[1]]++;
			else
				record.insert(pair<int, int>(nums1[i], 1));
			
		}
		vector<int> vectorResult;
		for(int i = 0; i < nums2.size(); i++){
			if(record.find(nums2[i]) != record.end() && record[nums2[i]] > 0){
				vectorResult.push_back(nums2[i]);
				record[nums2[i]]--;
				if(record[nums2[i]] == 0){
					record.erase(nums2[i]);
				}
			}
		}

		return vectorResult;
	 
    }
};
